﻿using System;
using System.ComponentModel;
using System.Configuration;
using Microsoft.Practices.Prism.ViewModel; 
using ZhiHeng.OneCard.WpfClient.ActionService;
using ZhiHeng.OneCard.WpfClient.Forms;
using Application = System.Windows.Application;
using @operator = ZhiHeng.Consume.DataContract.@operator;
using System.Reflection;

namespace ZhiHeng.OneCard.WpfClient.ViewModel
{
    /// <summary>
    /// Base class for all ViewModel classes in the application.
    /// It provides support for property change notifications 
    /// and clean up of resources such as event instanceHandlers. This class is abstract.
    /// </summary>
    public class ViewModelBase : NotificationObject
    {
         
        public static string pattern = "^(0|[1-9][0-9]*)(.[0-9]{1,2})?$";

        private bool _enableAutoReadCard = false;

        /// <summary>
        /// 是否自动读卡
        /// </summary>
        public bool EnableAutoReadCard
        {
            get { return _enableAutoReadCard; }
            set { _enableAutoReadCard = value; }
        }


        private bool _readCardContent = true;

        /// <summary>
        /// 是否读卡内容
        /// </summary>
        public bool ReadCardContent
        {
            get { return _readCardContent; }
            set { _readCardContent = value; }
        } 

        /// <summary>
        /// 窗体被隐藏时
        /// </summary>
        public virtual void OnUnActivated()
        { 
        }


        /// <summary>
        /// 窗体被显示时
        /// </summary>
        public virtual void OnActivated()
        { 
        }
 


        #region Constructor / Fields 

        private string _DisplayName;

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected ViewModelBase()
        {
        }
        /// <summary>
        /// Construct with display name.
        /// </summary>
        protected ViewModelBase(string displayName) 
        {
            _DisplayName = displayName;        
        }

        #endregion // Constructor
        
        #region Public Properties

        /// <summary>
        /// Get or set the display name.
        /// </summary>
        public string DisplayName
        {
            get { return _DisplayName ?? (_DisplayName = this.GetType().Name); }
            set 
            {
                SetPropertyValue(value, ref _DisplayName, "DisplayName"); 
            }
        } 

        #endregion // Constructor

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Generic method to set a property value with equality 
        /// checking and raising the property changed event.
        /// </summary>
        internal protected bool SetPropertyValue<T>(T value, ref T field, string propertyName)
        {
            if ((value != null && !value.Equals(field)) || (value == null && field != null))
            {
                field = value;
                if(propertyName != null)
                    RaisePropertyChanged(propertyName);
                return true;
            }
            return false;
        }

        
        #endregion // INotifyPropertyChanged Members


        #if DEBUG
        /// <summary>
        /// Useful for ensuring that ViewModel objects are properly garbage collected.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")]
        ~ViewModelBase()
        {
            string msg = string.Format("{0} ({1}) ({2}) Finalized", this.GetType().Name, this.DisplayName, this.GetHashCode());
            System.Diagnostics.Debug.WriteLine(msg);
        }
        #endif
        //private WCF.OneCardParkEntities _wcfOneCardEntity;
        //public WCF.OneCardParkEntities WcfOneCardEntity
        //{
        //    set { _wcfOneCardEntity = value; this.RaisePropertyChanged("WcfOneCardEntity"); }
        //    get
        //    {
        //        if (_wcfOneCardEntity == null)
        //            _wcfOneCardEntity = new WCF.OneCardParkEntities(new Uri(ConfigurationManager.AppSettings["WcfDataService"]),true);
        //        return _wcfOneCardEntity;
        //    }
        //}

        private ActionServiceClient _wcfOneCardActionService;
        public ActionServiceClient WcfOneCardActionService
        {
            set { _wcfOneCardActionService = value; this.RaisePropertyChanged("WcfOneCardActionService"); }
            get
            {
                if (_wcfOneCardActionService == null)
                    _wcfOneCardActionService = new ActionServiceClient();
                return _wcfOneCardActionService;
            }
        }

        private static @operator _userinfo;

        public static @operator UserInfo
        {
            get { return _userinfo ?? (_userinfo = new @operator()); }
            set { _userinfo = value; }

        }

        /// <summary>
        /// 提示窗
        /// </summary>
        /// <param name="msg">错误信息</param>
        /// <param name="msgtype">错误信息类型：true：提示信息false：错误信息</param>
        ///  <param name="clicknum">停留时间,默认2秒</param>
        public void ShowMsg(string msg = "", bool msgtype = true, int clicknum = 2000)
        {
            if (!System.Windows.Application.Current.Dispatcher.CheckAccess())
            {
                System.Windows.Application.Current.Dispatcher.Invoke(
                    new Action(delegate
                                   {
                                       MessageControl MC = new MessageControl(msg, clicknum, msgtype);
                                       MC.Owner = Application.Current.MainWindow;
                                       MC.Show();
                                   }
                        ));
            }
            else
            {
                MessageControl MC = new MessageControl(msg, clicknum, msgtype);
                MC.Owner = Application.Current.MainWindow;
                MC.Show();
            }
        }


        public  void ShowActionResult(int ret, int retCode)
        {
            if (ret == CommUtility.ErrorCode.OK && retCode == CommUtility.RetCode.RET_CODE_OK)
            {
                ShowMsg("操作成功！");

            }
            else if (ret != CommUtility.ErrorCode.OK)
            {
                ShowMsg("操作失败,【" + (ZhiHeng.OneCard.CommUtility.RetCodeMsg.ErrorCodeEnum)ret + "】！", false,4000);
            }
            else if (retCode != CommUtility.RetCode.RET_CODE_OK)
            {
                ShowMsg("操作失败,【" + (ZhiHeng.OneCard.CommUtility.RetCodeMsg.RetCodeEnum)retCode + "】！",false,4000);
            }
        }
    }
}
